package hoang.hut.app.tool;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class ResizeView {

	public static int widthStand = 720;
	public static int heightStand = 1280;

	private int widthReal;
	private int heightReal;

	/** Using to get display information. */
	private DisplayMetrics metrics;

	/** Rate resize width. */
	private float ratioResizeWidth;

	/** Rate resize height. */
	private float ratioResizeHeight;

	/** Black hem width and height. */
	private int hemBlackWidth = 0, hemBlackHeight = 0;

	/** Status of hem. */
	public int statusHem = 0; // O: nothing, 1: hem by width, 2: hem by height

	/** Image left and right hem. */
	protected ImageView bgRight, bgLeft;

	/**
	 * Get rate resize width.
	 * 
	 * @return float
	 * */
	protected float getRatioResizeWidth() {
		return this.ratioResizeWidth;
	}

	/**
	 * Get rate resize height.
	 * 
	 * @return float
	 * */
	protected float getRatioResizeHeight() {
		return this.ratioResizeHeight;
	}

	/**
	 * Get max width.
	 * 
	 * @return int
	 * */
	protected int getWidthReal() {
		return this.widthReal;
	}

	/**
	 * Get max height.
	 * 
	 * @return int
	 * */
	protected int getHeightReal() {
		return this.heightReal;
	}

	/**
	 * Get black hem width.
	 * 
	 * @return int
	 * */
	protected int getHemBlackWidth() {
		return this.hemBlackWidth;
	}

	protected float getDensity() {
		return this.metrics.density;
	}

	/**
	 * Get black hem height.
	 * 
	 * @return int
	 * */
	protected int getHemBlackHeight() {
		return this.hemBlackHeight;
	}

	private Activity activity;

	public ResizeView(Activity activity) {
		this.activity = activity;
		
		calculatorRatioResize();
	}

	public void calculatorRatioResize() {
		// get metric of display
		metrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

		widthReal = metrics.widthPixels;
		heightReal = metrics.heightPixels;

		if (widthReal > heightReal) {
			int temp = widthReal;
			widthReal = heightReal;
			heightReal = temp;
		}
		
		// calculate ratio resize view with display
		ratioResizeWidth = (float) (widthReal) / widthStand;
		ratioResizeHeight = (float) (heightReal) / heightStand;

		// calculate pixels appear black hem
		if (ratioResizeHeight < ratioResizeWidth) {
			statusHem = 1;

			// in case height < with, black hem will appear two slides
			hemBlackWidth = Math.round((widthReal - widthStand
					* ratioResizeHeight) / 2);

			// in this case, ratio resize actual by width must be calculated
			// again
			ratioResizeWidth = (float) ((widthReal) - 2 * hemBlackWidth)
					/ widthStand;
		} else {
			if (ratioResizeHeight > ratioResizeWidth) {
				statusHem = 2;

				// if height > width, black hem will appear above and below
				// display
				hemBlackHeight = Math.round((heightReal - heightStand
						* ratioResizeWidth) / 2);

				// in this case, ratio resize actual by height must be
				// calculated again
				ratioResizeHeight = (float) ((heightReal) - 2 * hemBlackHeight)
						/ heightStand;
			}
		}

//		Log.d("RESIZE", "kich thuoc chuan: " + widthStand + "," + heightStand);
//		Log.d("RESIZE", "kich thuoc that: " + widthReal + "," + heightReal);
//		Log.d("RESIZE", "ti le resize: " + ratioResizeWidth + ","
//				+ ratioResizeHeight);
//		Log.d("RESIZE", "hemblack: " + hemBlackWidth + "," + hemBlackHeight);
//		Log.d("RESIZE", "density: " + getDensity());
	}
	
	/**
	 * resize ảnh bitmap
	 * 
	 * @param bm
	 * @return: ảnh bitmap đã được resize
	 */
	public Bitmap resizeBitmap(Bitmap bm) {

		int width = bm.getWidth();
		int height = bm.getHeight();

		Log.d("BITMAP", "Size: "+ width+ ","+ height);

		Matrix matrix = new Matrix();
		matrix.postScale(ratioResizeWidth, ratioResizeHeight);

		Bitmap newBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
				false);
		Log.d("BITMAP", "New Size: "+ newBitmap.getWidth()+","+ newBitmap.getHeight());
		return newBitmap;
	}
	
	/**
	 * resize imageview
	 * 
	 * @param imgView
	 */
	public void resizeImageView(ImageView imgView) {

		Bitmap bm = ((BitmapDrawable)imgView.getDrawable()).getBitmap();
		
		int width = bm.getWidth();
		int height = bm.getHeight();

		Log.d("BITMAP", "Size: "+ width+ ","+ height);

		Matrix matrix = new Matrix();
		matrix.postScale(ratioResizeWidth, ratioResizeHeight);

		Bitmap newBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
				false);
		Log.d("BITMAP", "New Size: "+ newBitmap.getWidth()+","+ newBitmap.getHeight());
		
		imgView.setImageBitmap(newBitmap);
	}
	
	public void resizeView(final View v) {
		int h = (int) (v.getLayoutParams().height * ratioResizeHeight);
		int w = (int) (v.getLayoutParams().width *ratioResizeWidth);
		
		Log.d("RESIZE", "kich thuoc cu: "+v.getLayoutParams().width+","+v.getLayoutParams().height);
		Log.d("RESIZE", "kich thuoc moi: "+w+","+h);
		
		v.getLayoutParams().height = h;
		v.getLayoutParams().width = w;
	}

}
